//
// CDXGraph.h
//

#ifndef __H_CDXGraph__
#define __H_CDXGraph__

#include <streams.h>

#include <initguid.h>
#include "..\..\FilterTitleOverlay\ITitleOverlay.h"
#include "FileWriter.h"


// Filter graph notification to the specified window

class CDXGraph
{
private:
	IGraphBuilder *     mGraph;  
	IMediaControl *		mMediaControl;
	IMediaEventEx *		mEvent;
	IBasicVideo *		mBasicVideo;
	IBasicAudio *		mBasicAudio;
	IVideoWindow  *		mVideoWindow;
	IMediaSeeking *		mSeeking;

	DWORD				mObjectTableEntry; 

public:
	CDXGraph();
	virtual ~CDXGraph();

public:
	virtual bool Create(void);
	virtual void Release(void);
	virtual bool Attach(IGraphBuilder * inGraphBuilder);

	IGraphBuilder * GetGraph(void); // Not outstanding reference count
	IMediaEventEx * GetEventHandle(void);

	bool ConnectFilters(IPin * inOutputPin, IPin * inInputPin, const AM_MEDIA_TYPE * inMediaType = 0);
	void DisconnectFilters(IPin * inOutputPin);

	bool SetDisplayWindow(HWND inWindow);
	bool SetNotifyWindow(HWND inWindow);
	bool ResizeVideoWindow(long inLeft, long inTop, long inWidth, long inHeight);
	void HandleEvent(WPARAM inWParam, LPARAM inLParam);

	bool Run(void);        // Control filter graph
	bool Stop(void);
	bool Pause(void);
	bool IsRunning(void);  // Filter graph status
	bool IsStopped(void);
	bool IsPaused(void);

	bool SetFullScreen(BOOL inEnabled);
	bool GetFullScreen(void);

	// IMediaSeeking
	bool GetCurrentPosition(double * outPosition);
	bool GetStopPosition(double * outPosition);
	bool SetCurrentPosition(double inPosition);
	bool SetStartStopPosition(double inStart, double inStop);
	bool GetDuration(double * outDuration);
	bool SetPlaybackRate(double inRate);

	// Attention: range from -10000 to 0, and 0 is FULL_VOLUME.
	bool SetAudioVolume(long inVolume);
	long GetAudioVolume(void);
	// Attention: range from -10000(left) to 10000(right), and 0 is both.
	bool SetAudioBalance(long inBalance);
	long GetAudioBalance(void);

	HRESULT RenderFile(CString inFile);
	bool SnapshotBitmap(CString outFile);

	void AddToObjectTable(void) ;
	void RemoveFromObjectTable(void);



public:
	
	bool QueryInterfaces(void);


	// For Title Overlay

public:
	HRESULT WriteFileWithTitleOverlay(CString inFile, CString outFile);
	HRESULT RenderFileWithVideoRendererOld(CString inFile);
	HRESULT RenderFileWithTitleOverlay(CString inFile);
	HRESULT SetScoreFont();
	HRESULT ChangeScore(CString strScore);
private:
	HRESULT FindFilterByInterface(REFIID riid, IBaseFilter** ppFilter);
	HRESULT NextUpstream(IBaseFilter* pFilter, IBaseFilter** ppNext);
	HRESULT InsertTitleOverlayFilter(IBaseFilter* pFilter, IBaseFilter* pTransform);
	HRESULT TryInsertTitleOverlayFilter(IBaseFilter* pTilteOverlayFilter, 
		IPin* pPinOut, IPin* pPinIn, AM_MEDIA_TYPE* pMediaType);
	HRESULT SetTiltleOverlayStyle();
	HRESULT CreateTitleOverlayFilter();

	HRESULT SubstituteVideoRendererWithOld(IBaseFilter* pVideoRender, IBaseFilter* pVideoRenderOld);
	HRESULT SubstituteRenderers(IBaseFilter* pFileWriter, IBaseFilter* pVideoRender, IBaseFilter* pAudioRender);
	
	bool ShowSelectFontDlg(LOGFONT* pFont);

	IPin* GetPin(IBaseFilter* pFilter, PIN_DIRECTION dirRequest);
	IPin* GetPin(IBaseFilter* pFilter, PIN_DIRECTION dirRequest, GUID mediaType);
	// Helper methods
	IPin* InputPinOf(IBaseFilter* pFilter) 
	{
		return GetPin(pFilter, PINDIR_INPUT);
	}
	IPin* OutputPinOf(IBaseFilter* pFilter) 
	{
		return GetPin(pFilter, PINDIR_OUTPUT);
	}

	IPin* InputVideoPinOf(IBaseFilter* pFilter) 
	{
		return GetPin(pFilter, PINDIR_INPUT, MEDIATYPE_Video);
	}

	IPin* InputAudioPinOf(IBaseFilter* pFilter) 
	{
		return GetPin(pFilter, PINDIR_INPUT, MEDIATYPE_Audio);
	}


	HRESULT DropFilter(IBaseFilter* pFilter, IPin** ppPinUpstream);

	void EnumMediaTypes(IPin* pPin);
	void PrintMediaType(AM_MEDIA_TYPE* pMediaType);

	IBaseFilter* mTitleOverlay;
	ITitleOverlay* mTitleOverlaySetting;

	IBaseFilter* m_pVideoRenderOld;


private:
	FileWriter m_fileWriter;
};

#endif // __H_CDXGraph__